/**
 * 
 */
package com.cp16.tss.form;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.cp16.tss.handler.ServiceHandler;

/**
 * @author GiangHN
 * 
 */
public class ServiceForm extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtServiceName;
	private JTable tblResultSearch;
	private JPanel panel;
	private JLabel lblServiceId;
	@SuppressWarnings("rawtypes")
	private JComboBox cbServiceId;
	private JLabel lblServiceName;
	private JLabel lblListForService;
	private JButton btnSearch;
	private JPanel panel_1;
	private JButton btnAdd;
	private JButton btnEdit;
	private JButton btnDelete;

	/**
	 * Create the frame.
	 */
	public ServiceForm() {
		loadLookAndFeel();
		initComponents();
	}

	private void loadLookAndFeel() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException ex) {
			Logger.getLogger(ServiceForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (InstantiationException ex) {
			Logger.getLogger(ServiceForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (IllegalAccessException ex) {
			Logger.getLogger(ServiceForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (UnsupportedLookAndFeelException ex) {
			Logger.getLogger(ServiceForm.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	@SuppressWarnings("rawtypes")
	private void initComponents() {

		setTitle("CustomerCare- created by Hoang Ngoc Giang");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(280, 150, 731, 449);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(0, 191, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		panel = new JPanel();
		panel.setBounds(10, 11, 705, 94);
		contentPane.add(panel);
		panel.setLayout(null);

		lblServiceId = new JLabel("ID");
		lblServiceId.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblServiceId.setBounds(30, 41, 31, 30);
		panel.add(lblServiceId);

		txtServiceName = new JTextField();
		txtServiceName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtServiceName.setBounds(211, 42, 357, 30);
		panel.add(txtServiceName);
		txtServiceName.setColumns(10);

		cbServiceId = new JComboBox();
		cbServiceId.setFont(new Font("Tahoma", Font.PLAIN, 13));
		cbServiceId.setBounds(60, 41, 66, 30);
		cbServiceId.addActionListener(this);
		panel.add(cbServiceId);

		lblServiceName = new JLabel("Name");
		lblServiceName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblServiceName.setBounds(154, 41, 50, 30);
		panel.add(lblServiceName);

		lblListForService = new JLabel("List for Service");
		lblListForService.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblListForService.setBounds(279, 11, 174, 19);
		panel.add(lblListForService);

		btnSearch = new JButton("Search");
		btnSearch.setBackground(new Color(230, 230, 250));
		btnSearch.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnSearch.setBounds(578, 41, 83, 30);
		btnSearch.addActionListener(this);
		btnSearch.addActionListener(this);
		panel.add(btnSearch);

		panel_1 = new JPanel();
		panel_1.setBounds(10, 109, 705, 301);
		contentPane.add(panel_1);
		panel_1.setLayout(null);

		tblResultSearch = new JTable();
		tblResultSearch.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tblResultSearch.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "ID", "Name", "Priority", "No", "Street name",
						"District", "City", "Phone" }));
		// tblResultSearch.getColumnModel().getColumn(2).setPreferredWidth(148);
		// tblResultSearch.getColumnModel().getColumn(3).setPreferredWidth(108);

		tblResultSearch.setBounds(10, 11, 685, 231);
		panel_1.add(tblResultSearch);

		btnAdd = new JButton("Add");
		btnAdd.setBackground(new Color(230, 230, 250));
		btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnAdd.setBounds(240, 253, 77, 30);
		btnAdd.addActionListener(this);
		panel_1.add(btnAdd);

		btnEdit = new JButton("Edit");
		btnEdit.setBackground(new Color(230, 230, 250));
		btnEdit.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnEdit.setBounds(327, 253, 77, 30);
		btnEdit.addActionListener(this);
		panel_1.add(btnEdit);

		btnDelete = new JButton("Delete");
		btnDelete.setBackground(new Color(230, 230, 250));
		btnDelete.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnDelete.setBounds(414, 253, 77, 30);
		btnDelete.addActionListener(this);
		panel_1.add(btnDelete);

	}

	/**
	 * @return the txtServiceName
	 */
	public JTextField getTxtServiceName() {
		return txtServiceName;
	}

	/**
	 * @param txtServiceName
	 *            the txtServiceName to set
	 */
	public void setTxtServiceName(JTextField txtServiceName) {
		this.txtServiceName = txtServiceName;
	}

	/**
	 * @return the tblResultSearch
	 */
	public JTable getTblResultSearch() {
		return tblResultSearch;
	}

	/**
	 * @param tblResultSearch
	 *            the tblResultSearch to set
	 */
	public void setTblResultSearch(JTable tblResultSearch) {
		this.tblResultSearch = tblResultSearch;
	}

	/**
	 * @return the lblServiceId
	 */
	public JLabel getLblServiceId() {
		return lblServiceId;
	}

	/**
	 * @param lblServiceId
	 *            the lblServiceId to set
	 */
	public void setLblServiceId(JLabel lblServiceId) {
		this.lblServiceId = lblServiceId;
	}

	/**
	 * @return the cbServiceId
	 */
	@SuppressWarnings("rawtypes")
	public JComboBox getCbServiceId() {
		return cbServiceId;
	}

	/**
	 * @param cbServiceId
	 *            the cbServiceId to set
	 */
	@SuppressWarnings("rawtypes")
	public void setCbServiceId(JComboBox cbServiceId) {
		this.cbServiceId = cbServiceId;
	}

	/**
	 * @return the lblServiceName
	 */
	public JLabel getLblServiceName() {
		return lblServiceName;
	}

	/**
	 * @param lblServiceName
	 *            the lblServiceName to set
	 */
	public void setLblServiceName(JLabel lblServiceName) {
		this.lblServiceName = lblServiceName;
	}

	/**
	 * @return the lblListForService
	 */
	public JLabel getLblListForService() {
		return lblListForService;
	}

	/**
	 * @param lblListForService
	 *            the lblListForService to set
	 */
	public void setLblListForService(JLabel lblListForService) {
		this.lblListForService = lblListForService;
	}

	/**
	 * @return the btnSearch
	 */
	public JButton getBtnSearch() {
		return btnSearch;
	}

	/**
	 * @param btnSearch
	 *            the btnSearch to set
	 */
	public void setBtnSearch(JButton btnSearch) {
		this.btnSearch = btnSearch;
	}

	/**
	 * @return the btnAdd
	 */
	public JButton getBtnAdd() {
		return btnAdd;
	}

	/**
	 * @param btnAdd
	 *            the btnAdd to set
	 */
	public void setBtnAdd(JButton btnAdd) {
		this.btnAdd = btnAdd;
	}

	/**
	 * @return the btnEdit
	 */
	public JButton getBtnEdit() {
		return btnEdit;
	}

	/**
	 * @param btnEdit
	 *            the btnEdit to set
	 */
	public void setBtnEdit(JButton btnEdit) {
		this.btnEdit = btnEdit;
	}

	/**
	 * @return the btnDelete
	 */
	public JButton getBtnDelete() {
		return btnDelete;
	}

	/**
	 * @param btnDelete
	 *            the btnDelete to set
	 */
	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		ServiceHandler serviceHandler = new ServiceHandler();
		// search
		if (e.getSource() == btnSearch) {
			serviceHandler.loadTable(this);
			serviceHandler.loadValueComboBoxServiceId(this);
			// add
		} else if (e.getSource() == btnAdd) {
			ServiceAddForm serviceAddForm = new ServiceAddForm();
			serviceAddForm.setVisible(true);
			// this.getTblResultSearch().repaint();
			// edit
		} else if (e.getSource() == btnEdit) {
			serviceHandler.loadEditServiceForm(this);
			// delete
		} else if (e.getSource() == btnDelete) {
			serviceHandler.deleteServiceFromTable(this);
		} else if (e.getSource() == cbServiceId) {
			serviceHandler.displayServiceNameWhenSelectId(this);
		} else {

		}
	}
}
